<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>搜索</title>
    <link rel="stylesheet" href="../../icon/iconfont.css">
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/style.css" />
    <link rel="stylesheet" href="../../css/aui.css">
    <link rel="stylesheet" type="text/css" href="../../css/index.css" />
    <style>
        body {
            background-color: #FFF;
        }

        header {
            padding: 1.6rem 0.8rem 0.5rem 0.8rem;
            position: fixed;
            top: 0;
            width: 100%;
            background-color: #fff;
            z-index: 99;
            /*border-bottom: 1px solid #ccc*/
        }

        header span:nth-of-type(1) {
            font-size: 1rem
        }

        header span:nth-of-type(2) {
            font-size: 1rem
        }

        .aui-col-xs-4 {
            height: 4.7rem;
            padding: 0;
        }

        .aui-col-xs-4 img {
            width: 100%;
            height: 100%;
        }

        .max2line {
            margin-bottom: 0
        }

        .aui-card-list-content-padded {
            padding: 0;
            background-color: pink
        }

        .aui-card-list-content-padded video {
            width: 100%;
            height: 10rem
        }

        .footer_right .comment,
        .footer_right .like {
            font-size: 0.75rem
        }

        .zan_icon.active {
            color: rgb(201, 22, 26);
        }

        .video {
            width: 100%;
            height: 10rem;
            background-color: #000;
            border: none;
        }
    </style>
</head>

<body>
    <!-- 头部 -->
    <header>
        <span class="iconfont icon-fanhui" onClick="api.closeWin()"></span>
        <span>搜索</span>
    </header>
    <div class="wapper-box" id="newsListAll" style="background-color:#eee;padding-top:3.5rem">

    </div>
    <p style="text-align:center;margin-top:4rem;display:none" class="tishi">暂无更多数据~</p>
</body>
<script type="text/javascript" src="../../lib/zepto.js"></script>
<script type="text/javascript" src="../../lib/artTemplate/template.js"></script>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/dns.js"></script>
<script type="text/javascript" src="../../script/aui-toast.js"></script>
<script type="text/html" id="newsAll">
    {{each items as value index}}
    <div class="item" style="margin:0" articleID="{{value.id}}" onclick="btn_active1(this)">
        <div class="aui-card-list" style="margin-bottom:0.5rem">
            <div class="aui-card-list-header aui-card-list-user">
                <p class="max2line btn_activeId">{{value.title}}</p>
            </div>

            {{if value.news_type == 0}}
            <div class="aui-row aui-row-padded photoList" style="padding: 0 .8rem;">
                {{each items[index].head_img as val ind}}
                <div class="aui-col-xs-4" imgattr="{{val.filepath}}" onclick="bigPhoto(this)">
                    <!-- <img data-original="{{val.filepath}}" src="{{val.filepath}}" class="imgList" /> -->
                    <img class="imgList" style="width:100%;height:100%;background-image:url('{{val.original_image}}');background-size:cover" />
                </div>
                {{/each}}
            </div>
            {{/if}} {{if value.news_type == 1}}
            <div class="aui-row aui-row-padded" style="padding: 0 .8rem;">
                <!-- <video src="{{value.video_id.url}}" poster="{{value.head_img[0].filepath}}"></video> -->
                <video src="{{value.video_id.url}}" style='background-image:url("{{value.head_img[0].filepath}}");background-size:100%' class="video" poster="9999.jpg" controls x5-video-orientation="landscape" x5-playsinline="" playsinline="" webkit-playsinline=""></video>
            </div>
            {{/if}}


            <div class="flex-bottom" style="padding:0.5rem .8rem;">
                <div>
                    <span>{{value.read_num + value.virtual_read_num}}</span><span>阅读</span> &nbsp;&nbsp;
                    <span>{{value.create_time}}</span>
                </div>
                <div class="footer_right" articleID="{{value.id}}">
                    <span class="ping" onclick="getcomment(this)"><span class="iconfont icon-pinglunzhuanhuan" style="font-size:0.95rem;"></span><span class="comment">&nbsp;{{value.comment_num}}</span> &nbsp;</span>
                    {{if value.tid.is_awesome=="yes"}}
                    <span class="zan" onclick="getdianzan(this)"><span class="iconfont icon-dianzan zan_icon active" style="font-size:0.7rem;"></span>&nbsp;<span class="like">{{value.awesome_num+value.virtual_awesome_num}}</span></span>
                    {{/if}} {{if value.tid.is_awesome=="no"}}
                    <span class="zan" onclick="getdianzan(this)"><span class="iconfont icon-dianzan zan_icon" style="font-size:0.7rem;"></span>&nbsp;<span class="like">{{value.awesome_num+value.virtual_awesome_num}}</span></span>
                    {{/if}}
                </div>
            </div>
        </div>
    </div>
    {{/each}}
</script>
<script>
    apiready = function() {
        getsearchList();
        api.addEventListener({
            name: "reload"
        }, function(ret, err) {
            window.location.reload();
        })
    }
    var toast = new auiToast({})

    function getsearchList() {
        var search = localStorage.getItem("search");
        var token = localStorage.getItem("token");
        $.ajax({
            type: "get",
            url: dns + "news/search",
            beforeSend: function(request) {
                request.setRequestHeader("token", token)
            },
            data: {
                key: search,
                type: 1,
                page: 1,
                limit: 200
            },
            success: function(res) {
                // console.log(JSON.stringify(res));
                if (res.count == 0) {
                    $(".tishi").css("display", "block");
                    return false;
                }
                var html = template("newsAll", {
                    "items": res.data
                });
                document.getElementById("newsListAll").innerHTML = html;
                // 适配video宽高
                var width = $("video").width();
                var height = width / 16 * 9;
                $("video").height(height);
            },
            error: function(err) {
                console.log(JSON.parse(err.responseText).msg);
            }
        })
    };
    // 跳转文章详情
    function btn_active1(e) {
      //如果提供了事件对象，则这是一个非IE浏览器
      if ( e && e.stopPropagation ){
          //因此它支持W3C的stopPropagation()方法
        e.stopPropagation();
      }
      else {
          //否则，我们需要使用IE的方式来取消事件冒泡
          window.event.cancelBubble = true;
      }
            var articleID = $(e).attr("articleID");
            localStorage.setItem("articleID", articleID);
            // 跳转到其它页面暂停当前页面的视频
            getvideo()
            api.openWin({
                name: 'article',
                url: 'article.html',
                allowEdit: true,
                pageParam: {}, //页面代参数
                rect: {
                    x: 0,
                    y: 0,
                    w: api.winWidth,
                    h: api.winHeight
                }
            });
    }
    // 点赞，取消点赞
    function getdianzan(e) {
        //如果提供了事件对象，则这是一个非IE浏览器
        if (e && e.stopPropagation) {
            //因此它支持W3C的stopPropagation()方法
            e.stopPropagation();
        } else {
            //否则，我们需要使用IE的方式来取消事件冒泡
            window.event.cancelBubble = true;
        }
        // 获取此时的毫秒数
        var newTime = new Date();
        var getnewTime = newTime.getTime();
        // 获取登陆时的毫秒数
        var getloginTime = localStorage.getItem("getloginTime");
        var Time = getnewTime - getloginTime;
        var token = localStorage.getItem("token");
        var cacheTime = localStorage.getItem("cacheTime");
        if (Time > cacheTime || !token) {
            api.confirm({
                title: "当前游客状态",
                msg: "需要前往登录页面,是否继续?",
                buttons: ["是", "否"]
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    toast.loading({
                        title: "跳转中",
                        duration: 2000
                    }, function(ret) {
                        setTimeout(function() {
                            toast.hide();
                        }, 3000)
                    });
                    setTimeout(function() {
                        api.openWin({
                            name: 'login',
                            url: '../system/login.html',
                            allowEdit: true,
                            pageParam: {}, //页面代参数
                            rect: {
                                x: 0,
                                y: 0,
                                w: api.winWidth,
                                h: api.winHeight
                            }
                        });
                    }, 1000)
                }
            });
            return false;
        }
        var articleID = $(e).parent().attr("articleID");
        var val = $(e).find(".like").text();
        var that = e;
        $.ajax({
            type: "post",
            data: {
                type: 1,
                article: articleID
            },
            beforeSend: function(request) {
                request.setRequestHeader("token", token);
            },
            url: dns + "like/okorno",
            success: function(res) {
                if (res.msg == "成功点赞") {
                    $(that).find(".zan_icon").addClass("active");
                    val++;
                    $(that).find(".like").text(val);
                    toast.custom({
                        title: "成功点赞!",
                        html: '<i class="aui-iconfont aui-icon-laud"></i>',
                        duration: 1500
                    });
                }
                if (res.msg == "取消点赞") {
                    $(that).find(".zan_icon").removeClass("active");
                    val--;
                    $(that).find(".like").text(val);
                }
            }
        })
    }
    // 评论
    function getcomment(e) {
        //如果提供了事件对象，则这是一个非IE浏览器
        if (e && e.stopPropagation) {
            //因此它支持W3C的stopPropagation()方法
            e.stopPropagation();
        } else {
            //否则，我们需要使用IE的方式来取消事件冒泡
            window.event.cancelBubble = true;
        }
        var articleID = $(e).parent().attr("articleID");
        localStorage.setItem("articleID", articleID);

        api.openWin({
            name: 'article',
            url: 'article.html',
            allowEdit: true,
            pageParam: {}, //页面代参数
            rect: {
                x: 0,
                y: 0,
                w: api.winWidth,
                h: api.winHeight
            }
        });
    }
    // 图片放大
    function bigPhoto(e) {
      //如果提供了事件对象，则这是一个非IE浏览器
      if (e && e.stopPropagation) {
          //因此它支持W3C的stopPropagation()方法
          e.stopPropagation();
      } else {
          //否则，我们需要使用IE的方式来取消事件冒泡
          window.event.cancelBubble = true;
      }
                var ind = $(e).index();
                var imgList = [];
                $(e).parent().find(".aui-col-xs-4").each(function() {
                    imgList.push($(this).attr("imgattr"))
                })
                console.log(imgList);
                var photoBrowser = api.require('photoBrowser');
                photoBrowser.open({
                    images: imgList,
                    placeholderImg: 'widget://res/img/apicloud.png',
                    bgColor: '#000',
                    activeIndex: ind
                }, function(ret, err) {
                    if (ret.eventType == 'click') {
                        photoBrowser.close()
                    }
                    if (ret.eventType == 'longPress') {
                        var a = Math.floor(Math.random() * 1000);
                        var retIndex = ret.index;
                        api.confirm({
                            title: "温馨提示",
                            msg: "是否保存图片?",
                            buttons: ["是", "否"]
                        }, function(ret, err) {
                            if (ret.buttonIndex == 1) { //确认退出
                                api.download({
                                    url: imgList[retIndex],
                                    savePath: 'fs://' + a + '.jpg',
                                    report: true,
                                    cache: false
                                }, function(ret, err) {
                                    if (ret.state == 1) {
                                        //下载成功
                                        // console.log("下载成功");
                                        // 保存图片
                                        api.saveMediaToAlbum({
                                            path: ret.savePath
                                        }, function(ret, err) {
                                            if (ret.status == true) {
                                                alert("保存成功!")
                                            } else {}
                                        });
                                    } else {

                                    }
                                });
                            } else {
                                return;
                            }
                        });
                    } else {
                        // alert(JSON.stringify(err));
                    }
                });
    }

    function getvideo() {
        // 跳转到其它页面暂停当前页面的视频
        var videoList = document.getElementsByTagName("video");
        for (var i = 0; i < videoList.length; i++) {
            if (!videoList[i].paused) {
                videoList[i].pause(); //暂停控制
            }
        }
    }
</script>

</html>
